package com.industrial.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.system.domain.SysArea;
import com.industrial.system.mapper.SysAreaMapper;
import com.industrial.system.service.ISysAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *     行政区划数据Service实现类
 * </p>
 * @author SangGuangCan
 * @since 2024-01-16
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService {

    private SysAreaMapper sysAreaMapper;

    @Autowired
    public SysAreaServiceImpl(SysAreaMapper sysAreaMapper) {
        this.sysAreaMapper = sysAreaMapper;
    }

    /**
     * <p>
     * 查询行政区划数据的树形结构
     * </p>
     *
     * @return 树形结果
     */
    @Override
    public AjaxResult queryAreaTree() {
        List<SysArea> sysAreaArgs = sysAreaMapper.selectShanDongProvinceAreaDataList();
        if(sysAreaArgs != null && !sysAreaArgs.isEmpty()){
            //过滤掉所有的街道
            List<SysArea> tempSysAreaArgs = sysAreaArgs.stream().filter(item -> item.getAreaCode().length() == 6).collect(Collectors.toList());
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("code");
            treeNodeConfig.setParentIdKey("parentCode");
            treeNodeConfig.setWeightKey("code");
            treeNodeConfig.setDeep(4);
            List<Tree<String>> treeNodes = TreeUtil.<SysArea, String>build(tempSysAreaArgs, "0", treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getAreaCode());
                        tree.setParentId(treeNode.getParentCode());
                        tree.setName(treeNode.getBaseName());
                        tree.setWeight(treeNode.getAreaCode());
                        tree.putExtra("label",treeNode.getBaseName());
                        tree.putExtra("value",treeNode.getAreaCode());
                    });
            return AjaxResult.success(treeNodes);
        }
        return AjaxResult.success(new ArrayList<Tree<String>>());
    }

    @Override
    public AjaxResult queryAreaTreeByCode(SysArea sysArea) {
        String areaCode = sysArea.getAreaCode();
        List<SysArea> result = new ArrayList<>();
        if(areaCode.length() == 6){
            if(StringUtils.endsWith(areaCode,"0000")){
                List<SysArea> sysAreaArgs =  sysAreaMapper.selectShanDongProvinceAreaDataList();
                result.addAll(sysAreaArgs);
            }else if(StringUtils.endsWith(areaCode,"00")){
                List<SysArea> sysAreaArgs =  sysAreaMapper.selectCityProvinceAreaDataList(sysArea);
                if(sysAreaArgs != null && !sysAreaArgs.isEmpty()){
                    Map<String, String> mapPk = sysAreaArgs.stream()
                            .filter(item -> StringUtils.equals(item.getAreaCode(), sysArea.getAreaCode()))
                            .collect(Collectors.toMap(SysArea::getAreaCode, SysArea::getParentCode));
                    String pCode = mapPk.get(sysArea.getAreaCode());
                    SysArea sysArea1 = sysAreaMapper.selectAreaByPk(pCode);
                    sysAreaArgs.add(sysArea1);
                }
                result.addAll(sysAreaArgs);
            }else{
                SysArea xian = sysAreaMapper.selectAreaByPk(sysArea.getAreaCode());
                if(xian == null){
                    throw new RuntimeException("没有找到县数据");
                }
                result.add(xian);
                String xianParentCode = xian.getParentCode();
                SysArea city = sysAreaMapper.selectAreaByPk(xianParentCode);
                if(city == null){
                    throw new RuntimeException("没有找到市数据");
                }
                result.add(city);
                String cityParentCode = city.getParentCode();
                SysArea sheng = sysAreaMapper.selectAreaByPk(cityParentCode);
                if(sheng == null){
                    throw new RuntimeException("没有找到省数据");
                }
                result.add(sheng);
            }
        }else{
            throw new RuntimeException("请传入省市区类型行政区代码");
        }


        if(!result.isEmpty()){

            Map<String, String> areaMap = result.stream().collect(Collectors.toMap(SysArea::getAreaCode, SysArea::getParentCode));
            String parentCode = areaMap.get(sysArea.getAreaCode());

            List<SysArea> tempSysAreaArgs = result.stream().filter(item -> item.getAreaCode().length() == 6).collect(Collectors.toList());
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("code");
            treeNodeConfig.setParentIdKey("parentCode");
            treeNodeConfig.setWeightKey("code");
            treeNodeConfig.setDeep(4);
            List<Tree<String>> treeNodes = TreeUtil.<SysArea, String>build(tempSysAreaArgs, "0", treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getAreaCode());
                        tree.setParentId(treeNode.getParentCode());
                        tree.setName(treeNode.getBaseName());
                        tree.setWeight(treeNode.getAreaCode());
                        tree.putExtra("label",treeNode.getBaseName());
                        tree.putExtra("value",treeNode.getAreaCode());
                    });
            return AjaxResult.success(treeNodes);
        }

        return AjaxResult.success(result);
    }

    @Override
    public List<SysArea> getAreaByParentId(String parentId) {
        List<SysArea> tempSysAreaArgs = sysAreaMapper.selectAreaByParentId(parentId);
        return tempSysAreaArgs;
    }

    @Override
    public AjaxResult getAreaNameByCode(String code) {
        String areaName = sysAreaMapper.selectAreaNameByCode(code);
        return AjaxResult.success(areaName);
    }

    /**
     * @param parentCode
     * @return
     */
    @Override
    public AjaxResult queryAreaTreeByParentCode(String parentCode) {
        List<SysArea> sysAreaArgs = sysAreaMapper.selectShanDongProvinceAreaDataList();
        if(sysAreaArgs != null && !sysAreaArgs.isEmpty()){
            //过滤掉所有的街道
            List<SysArea> tempSysAreaArgs = sysAreaArgs.stream().filter(item -> item.getAreaCode().length() == 6)
//                    .filter(k -> {
//                if(StringUtils.equals(k.getAreaCode(),parentCode) || StringUtils.equals(k.getParentCode(),parentCode)){
//                    return true;
//                }
//                return false;
//            })
                    .collect(Collectors.toList());

            SysArea ss = sysAreaMapper.selectAreaParentCodeyCode(parentCode);

            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("code");
            treeNodeConfig.setParentIdKey("parentCode");
            treeNodeConfig.setWeightKey("code");
            treeNodeConfig.setDeep(4);
            List<Tree<String>> treeNodes = TreeUtil.<SysArea, String>build(tempSysAreaArgs, ss.getParentCode(), treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getAreaCode());
                        tree.setParentId(treeNode.getParentCode());
                        tree.setName(treeNode.getBaseName());
                        tree.setWeight(treeNode.getAreaCode());
                        tree.putExtra("label",treeNode.getBaseName());
                        tree.putExtra("value",treeNode.getAreaCode());
                    });
            return AjaxResult.success(treeNodes);
        }
        return AjaxResult.success(new ArrayList<Tree<String>>());
    }


    /**
     * 查询山东省的地市行政区
     *
     * @param parentId
     * @return
     */
    @Override
    public AjaxResult getAreaInfoByOnlyCity(String parentId) {
        List<SysArea> sysAreaArgs = sysAreaMapper.selectShanDongProvinceAreaDataList();
        if(sysAreaArgs != null && !sysAreaArgs.isEmpty()){
            List<SysArea> tempSysAreaArgs = sysAreaArgs.stream().filter(item -> item.getAreaCode().length() == 6).filter(item -> {
                if(StringUtils.endsWith(item.getAreaCode(),"0000") || StringUtils.endsWith(item.getAreaCode(),"00")){
                    return true;
                }
                return false;
            }).collect(Collectors.toList());

            SysArea ss = sysAreaMapper.selectAreaParentCodeyCode(parentId);

            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("code");
            treeNodeConfig.setParentIdKey("parentCode");
            treeNodeConfig.setWeightKey("code");
            treeNodeConfig.setDeep(4);
            List<Tree<String>> treeNodes = TreeUtil.<SysArea, String>build(tempSysAreaArgs, ss.getParentCode(), treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getAreaCode());
                        tree.setParentId(treeNode.getParentCode());
                        tree.setName(treeNode.getBaseName());
                        tree.setWeight(treeNode.getAreaCode());
                        tree.putExtra("label",treeNode.getBaseName());
                        tree.putExtra("value",treeNode.getAreaCode());
                    });
            return AjaxResult.success(treeNodes);
        }
        return AjaxResult.success(new ArrayList<Tree<String>>());
    }

    /**
     * @param parentCode
     * @return
     */
    @Override
    public AjaxResult queryAreaAllareaTreeByParentCode(String parentCode) {
        List<SysArea> sysAreaArgs = sysAreaMapper.selectShanDongProvinceAreaDataList();
        if(sysAreaArgs != null && !sysAreaArgs.isEmpty()){
            //过滤掉所有的街道
            List<SysArea> tempSysAreaArgs = sysAreaArgs.stream().filter(item -> item.getAreaCode().length() == 6).filter(k -> {
                String areaCode = k.getAreaCode();
                String parentCode1 = k.getParentCode();
                if(!StringUtils.endsWith(parentCode,"0000") && !StringUtils.endsWith(parentCode,"00")){
                    if(StringUtils.equals(parentCode,areaCode)){
                        return true;
                    }else{
                        return false;
                    }
                }else if(!StringUtils.endsWith(parentCode,"0000") && StringUtils.endsWith(parentCode,"00")){
                    if(StringUtils.equals(parentCode,areaCode) || StringUtils.equals(parentCode1,parentCode)){
                        return true;
                    }else{
                        return false;
                    }
                }
                return true;
            }).collect(Collectors.toList());

            SysArea ss = sysAreaMapper.selectAreaParentCodeyCode(parentCode);

            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("code");
            treeNodeConfig.setParentIdKey("parentCode");
            treeNodeConfig.setWeightKey("code");
            treeNodeConfig.setDeep(4);
            List<Tree<String>> treeNodes = TreeUtil.<SysArea, String>build(tempSysAreaArgs, ss.getParentCode(), treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getAreaCode());
                        tree.setParentId(treeNode.getParentCode());
                        tree.setName(treeNode.getBaseName());
                        tree.setWeight(treeNode.getAreaCode());
                        tree.putExtra("label",treeNode.getBaseName());
                        tree.putExtra("value",treeNode.getAreaCode());
                    });
            return AjaxResult.success(treeNodes);
        }
        return AjaxResult.success(new ArrayList<Tree<String>>());
    }

    @Override
    public List<String> queryAreaCodeByAreaName(String areaName) {
        List<String> r = sysAreaMapper.selectAreaCodeByAreaName(areaName);
        return r;
    }
}
